﻿/*
 * UNDONE
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BLL;
using DA;
using DevComponents.DotNetBar;

namespace GUI.View
{
    public partial class TuyChinh : UserControl
    {
        private MyQueryDA myQueryDA;
        private ThamSoBLL thamsoBLL;
        public string usersname;
        private AppCommands _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                btnCancel.Command = newValue.TuyChinhCommands.Cancel;
            }
            else
            {
                btnCancel.Command = null;
            }
        }

        public TuyChinh()
        {
            InitializeComponent();
            myQueryDA = new MyQueryDA();
            thamsoBLL = new ThamSoBLL();
        }

        private void btnAddAdmin_Click(object sender, EventArgs e)
        {
            if (txtTenTaiKhoan.Text.Equals("") || txtMatKhau.Text.Equals("") || txtMatKhauXacNhan.Text.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin.", eToastPosition.MiddleCenter);
                txtTenTaiKhoan.Focus();
                return;
            }
            string query = String.Format("select id from DANGNHAP where TaiKhoan ='{0}'", txtTenTaiKhoan.Text);
            if (myQueryDA.getId(query) > 0)
            {
                ToastNotification.Show(this, "Tài khoản đã tồn tại.", eToastPosition.MiddleCenter);
                txtTenTaiKhoan.Focus();
                return;
            }

            if (!txtMatKhau.Text.Equals(txtMatKhauXacNhan.Text))
            {
                ToastNotification.Show(this, "Mật khẩu không hợp lệ.", eToastPosition.MiddleCenter);
                txtMatKhau.Focus();
                return;
            }

            query = String.Format("insert into DANGNHAP(TaiKhoan,MatKhau) values('{0}','{1}')", txtTenTaiKhoan.Text, txtMatKhau.Text);
            if (myQueryDA.doNonQuery(query))
            {
                ToastNotification.Show(this, "Thêm thành công.", eToastPosition.MiddleCenter);
                txtTenTaiKhoan.Text = "";
                txtMatKhau.Text = "";
                txtMatKhauXacNhan.Text = "";
                txtTenTaiKhoan.Focus();
            }
            else
            {
                ToastNotification.Show(this, "Failed.", eToastPosition.MiddleCenter);
            }
        }

        private void btnEditPassword_Click(object sender, EventArgs e)
        {
            string _newpassword = txtNewPassword.Text.Trim();
            string _renewpassword = txtReNewPassword.Text.Trim();

            string query = String.Format("select id from DANGNHAP where TaiKhoan = '{0}' and MatKhau = '{1}'", txtUsersname.Text.Trim(), txtMk.Text.Trim());
            if (myQueryDA.getId(query) == 0)
            {
                ToastNotification.Show(this, "Mật khẩu không hợp lệ.", eToastPosition.MiddleCenter);
                txtMk.Text = "";
                txtMk.Focus();
                return;
            }
            if (_newpassword.Equals("") || _renewpassword.Equals(""))
            {
                ToastNotification.Show(this, "Vui lòng nhập đầy đủ thông tin.", eToastPosition.MiddleCenter);
                return;
            }
            if (!_newpassword.Equals(_renewpassword))
            {
                ToastNotification.Show(this, "Vui lòng nhập lại mật khẩu.", eToastPosition.MiddleCenter);
                txtNewPassword.Text = "";
                txtReNewPassword.Text = "";
                txtMk.Text = "";
                return;
            }

            query = String.Format("update DANGNHAP set MatKhau ='{0}' where TaiKhoan = '{1}'", txtNewPassword.Text, txtUsersname.Text);
            if (myQueryDA.doNonQuery(query))
            {
                ToastNotification.Show(this, "Đổi mật khẩu thành công.", eToastPosition.MiddleCenter);
                txtNewPassword.Text = "";
                txtReNewPassword.Text = "";
                txtMk.Text = "";
            }
            else
                ToastNotification.Show(this, "Failed.", eToastPosition.MiddleCenter);

        }

        private void txtSoNgayDangKy_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaTienDangKy_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoTuMoTaNoiDung_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaTienGiam_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == 46)
                e.Handled = false;
        }

        private void btnThayDoiQuyDinh3_Click(object sender, EventArgs e)
        {
            //string updateSotuNoiDungQC = String.Format("update THAMSO set SoTuMoTaNoiDungQuangCaoToiDa='{0}'", Int32.Parse(txtSoTuMoTaNoiDung.Text));
            if (thamsoBLL.SetSoTuMoTaQCToiDa(txtSoTuMoTaNoiDung.Text))
            {
                ToastNotification.Show(this, "Thành Công", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Lỗi Rồi", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnThayDoiQuyDinh4_Click(object sender, EventArgs e)
        {
            //string updateSongay_Sotien = String.Format("update THAMSO set SoNgayDangKy='{0}',GiaTienDangKy='{1}'", Int32.Parse(txtSoNgayDangKy.Text), float.Parse(txtGiaTienDangKy.Text));
            if (thamsoBLL.SetSoNgayDangKy(txtSoNgayDangKy.Text)&&thamsoBLL.SetGiaTienDangKy(txtGiaTienDangKy.Text))
            {
                ToastNotification.Show(this, "Thành Công", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Lỗi Rồi", eToastPosition.MiddleCenter);
                return;
            }
        }

        private void btnThayDoiQuyDinh5_Click(object sender, EventArgs e)
        {
            //string updateSongay_Sotien = String.Format("update THAMSO set GiaTienGiamToiDa='{0}'", float.Parse(txtGiaTienGiam.Text));
            if (thamsoBLL.SetGiaTienGiamToiDa(txtGiaTienGiam.Text))
            {
                ToastNotification.Show(this, "Thành Công", eToastPosition.MiddleCenter);
                return;
            }
            else
            {
                ToastNotification.Show(this, "Lỗi Rồi", eToastPosition.MiddleCenter);
                return;
            }
        }

    }
}
